Application
/* Name : main.c * Purpose : Source code for LCD Interfacing with ARM LPC1248. * Author : Gemicates * Date : 2018-02-02 * Website : www.gemicates.org * Revision : None */ #include <lpc21xx.h> // header file for LPC21XX series #define LCD (0xff<<8) #define RS (1<<16) // register select pin #define RW (1<<17) // read write pin #define EN (1<<18) // enable pin #define r1 (1<<16) #define r2 (1<<17) #define r3 (1<<18) #define r4 (1<<19) #define c1 (1<<20) #define c2 (1<<21) #define c3 (1<<22) #define c4 (1<<23) void delay(unsigned int time); // variable delay function void lcd_ini(void); void lcd_print(char *str); void lcd_cmd(unsigned char command); void lcd_dat(unsigned int data); unsigned char keypad (void); void keypad_delay(void); int main (void) { PINSEL0 = 0x00000000; // select PORT0 as GPIO mode IODIR0 = 0Xffffffff; // make PORT0 pin as Output mode PINSEL1 = 0x00000000; // select PORT1 as GPIO mode IODIR1 = 0x00f00000; lcd_ini(); lcd_print("Press any key"); lcd_cmd(0xc0); // bring cursor to second ROW while(1) // Repeat(loop) forever { lcd_dat(keypad()); } return 0; } void keypad_delay(void) // delay function for keypad { unsigned int t1,t2; for(t1=0;t1<300;t1++) for(t2=0;t2<1275;t2++); } unsigned char keypad (void) // keypad operation function { unsigned char key; IOCLR1|=(c1|c2|c3|c4|r1|r2|r3|r4); while(1) { IOCLR1|=c1; IOSET1|=(c2|c3|c4); // first column = 0 if((IOPIN1&r1)==0) { key='7'; keypad_delay(); return key; } else if((IOPIN1&r2)==0) { key='8'; keypad_delay(); return key; } else if((IOPIN1&r3)==0) { key='9'; keypad_delay(); return key; } else if((IOPIN1&r4)==0) { key='/'; keypad_delay(); return key; } IOCLR1|=c2; IOSET1|=(c1|c3|c4); // second column = 0 if((IOPIN1&r1)==0) { key='4'; keypad_delay(); return key; } else if((IOPIN1&r2)==0) { key='5'; keypad_delay(); return key; } else if((IOPIN1&r3)==0) { key='6'; keypad_delay(); return key; } else if((IOPIN1&r4)==0) { key='*'; keypad_delay(); return key; } IOCLR1|=c3; IOSET1|=(c1|c2|c4); // third column = 0 if((IOPIN1&r1)==0) { key='1'; keypad_delay(); return key; } else if((IOPIN1&r2)==0) { key='2'; keypad_delay(); return key; } else if((IOPIN1&r3)==0) { key='3'; keypad_delay(); return key; } else if((IOPIN1&r4)==0) { key='-'; keypad_delay(); return key; } IOCLR1|=c4; IOSET1|=(c1|c2|c3); // forth column = 0 if((IOPIN1&r1)==0) { lcd_cmd(0x01); keypad_delay(); } else if((IOPIN1&r2)==0) { key='0'; keypad_delay(); return key; } else if((IOPIN1&r3)==0) { key='='; keypad_delay(); return key; } else if((IOPIN1&r4)==0) { key='+'; keypad_delay(); return key; } } } void lcd_cmd(unsigned char command) // Function to send command to LCD { IO0CLR|=(RS|RW|EN|LCD); IO0SET|=(command<<8); IO0CLR|=RS; IO0CLR|=RW; IO0SET|=EN; delay(2); IO0CLR|=EN; delay(3); } void lcd_dat(unsigned int data) // Function to send data to LCD { IO0CLR|=(RS|RW|EN|LCD); IO0SET|=(data<<8); IO0SET|=RS; IO0CLR|=RW; IO0SET|=EN; delay(2); IO0CLR|=EN; delay(3); } void lcd_print(char *str) { while(*str!='\0') { lcd_dat(*str); str++; } } void lcd_ini(void) // Funtion to Initialize LCD { delay(5); lcd_cmd(0X38); // for using 8-bit 2 row mode and 5x7 Dots of LCD lcd_cmd(0X0e); // turn display ON for cursor blinking lcd_cmd(0X06); // display ON lcd_cmd(0X01); // clear screen delay(5); lcd_cmd(0X80); // clear screen } void delay(unsigned int time) // variable delay function { unsigned int t1,t2; for(t1=0;t1<time;t1++) for(t2=0;t2<1275;t2++); }